﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data.Common;
using NCT.Framework.Database;
using System.Data;

namespace VISS.API.Users {
    class UserAppearanceProfileBC : IUserAppearanceProfile, IUserAppearanceProfileAdmin {

        #region IUserAppearanceProfile Members

        public bool Insert(UserApperanceProfileEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {

                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Normalize data
                item.DateCreated = DateTime.Now;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                // Add User to DB
                result = UserAppearanceProfileDA.Insert(item, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Add User into Cache
                if (result && ConfigInfo.UseCache) {
                }
            }
            catch (Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserApperanceProfileBC.Insert({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public bool Update(UserApperanceProfileEnt item) {
            bool result = true;
            DbConnection dbCon = null;
            DbTransaction dbTrans = null;
            try {

                //Get connection name
                string connectionName = ConfigInfo.ConnectionName;

                //Normalize data
                item.DateModified = DateTime.Now;

                //Create connection & transaction
                dbCon = Database.CreateConnection(connectionName, true);
                dbTrans = dbCon.BeginTransaction();

                // Update User to DB
                result = UserAppearanceProfileDA.Update(item, connectionName, dbCon, dbTrans) > 0;

                //Add log
                if (result) {
                }

                // Update User into Cache
                if (result && ConfigInfo.UseCache) {
                }
            }
            catch (Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserApperanceProfileBC.Update({0}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            finally {
                if (dbTrans != null) {
                    if (result) {
                        dbTrans.Commit();
                    }
                    else {
                        // rollback
                        dbTrans.Rollback();
                    }
                }
                if (dbCon != null && dbCon.State == ConnectionState.Open) {
                    dbCon.Close();
                    
                }
            }
            return result;
        }

        public UserApperanceProfileEnt GetByUserKey(string userKey) {
            UserApperanceProfileEnt item = null;
            try {
                if (ConfigInfo.UseCache) {
                }
                else {
                    //Get connection name
                    string connectionName = ConfigInfo.ConnectionName;

                    item = UserAppearanceProfileDA.GetByUserKey(userKey, connectionName, null, null);
                }
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("VISS.Users.UserApperanceProfile.GetByUserKey({0}): \n" + ex.ToString(), userKey));
            }
            return item;
        }

        #endregion
    }
}
